/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package threadmatriz;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author julio
 */
public class FMatriz extends javax.swing.JFrame {

    /**
     * Creates new form FMatriz
     */
    public FMatriz() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        numeroThread = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        mTam = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        tempo = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        saida = new javax.swing.JTextArea();
        imprimir = new javax.swing.JCheckBox();
        matSeq = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        numeroThread.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }));
        numeroThread.setSelectedItem(10);
        numeroThread.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                numeroThreadActionPerformed(evt);
            }
        });

        jLabel1.setText("Quatidade de Threads");

        jLabel2.setText("Tamanho da Matriz");

        mTam.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                mTamInputMethodTextChanged(evt);
            }
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
        });

        jButton1.setText("Calcular Tthread");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel3.setText("Tempo");

        tempo.setText("00:00");

        saida.setColumns(20);
        saida.setRows(5);
        jScrollPane1.setViewportView(saida);

        imprimir.setText("Imprimir");

        matSeq.setText("Calculo Seq");
        matSeq.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                matSeqActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(tempo)
                                        .addComponent(jLabel2)))
                                .addGap(29, 29, 29)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(numeroThread, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(mTam)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(22, 22, 22)
                                .addComponent(jLabel3)
                                .addGap(115, 115, 115)
                                .addComponent(jLabel4)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(107, 107, 107)
                .addComponent(jButton1)
                .addGap(43, 43, 43)
                .addComponent(imprimir)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(matSeq)
                .addGap(28, 28, 28))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(numeroThread, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(mTam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(tempo))
                .addGap(13, 13, 13)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(imprimir)
                    .addComponent(matSeq))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 214, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void numeroThreadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_numeroThreadActionPerformed
       
    }//GEN-LAST:event_numeroThreadActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
      if(Integer.parseInt(numeroThread.getSelectedItem().toString())>Integer.parseInt(mTam.getText())){
          JOptionPane.showMessageDialog(null,"Nº de Threads maior que o numero de colunas", "Error", JOptionPane.ERROR_MESSAGE);
      }else{
        ThreadMatriz f = new ThreadMatriz();
        saida.setText(null);
        tempo.setText("");
        try {
        int tam = Integer.parseInt(mTam.getText());
        double aa[][] = new double[tam][tam];
        double bb[][] = new double[tam][tam];
        double dd[][] = new double[tam][tam];
        
        f.setTam(tam);
        f.setQtdThread(Integer.parseInt(numeroThread.getSelectedItem().toString()));
         f.runThread(aa,bb,dd);
         tempo.setText(f.getTempo());
        if(imprimir.isSelected()){
           
            saida.append(f.imprimeMatriz(bb));
            saida.append("----------------------------------------------\n");
            saida.append(f.imprimeMatriz(dd));
        }
            
            
     
     
        } catch (InterruptedException ex) {
            JOptionPane.showMessageDialog(null,ex.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
        } catch(NumberFormatException ex){
            JOptionPane.showMessageDialog(null,ex.getMessage()+" Matriz Invalida", "Error", JOptionPane.ERROR_MESSAGE);
        }
      }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void mTamInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_mTamInputMethodTextChanged
        // TODO add your handling code here:
          if(Integer.parseInt(numeroThread.getSelectedItem().toString())>Integer.parseInt(mTam.getText())){
          JOptionPane.showMessageDialog(null,"Nº de Threads maior que o numero de colunas", "Error", JOptionPane.ERROR_MESSAGE);
      }
    }//GEN-LAST:event_mTamInputMethodTextChanged

    private void matSeqActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_matSeqActionPerformed
      // TODO add your handling code here:
        int k = 1;
      if(Integer.parseInt(numeroThread.getSelectedItem().toString())>Integer.parseInt(mTam.getText())){
          JOptionPane.showMessageDialog(null,"Nº de Threads maior que o numero de colunas", "Error", JOptionPane.ERROR_MESSAGE);
      }else{
        Matrizseq f = new Matrizseq();
         ThreadMatriz g = new ThreadMatriz();
        saida.setText(null);
        tempo.setText("");
        try {
        int tam = Integer.parseInt(mTam.getText());
        double aa[][] = new double[tam][tam];
        double bb[][] = new double[tam][tam];
        double dd[][] = new double[tam][tam];
         for (int i = 0; i < tam; i++)
           for (int j = 0; j < tam; j++) {
               aa[i][j]= k;
               bb[i][j]= k;
               k++;
           }
         Long start = System.currentTimeMillis();
         f.multMatriz(aa, bb, dd, tam, 0, tam);
         Long end = System.currentTimeMillis();
         tempo.setText(new SimpleDateFormat("mm:ss").format(new Date(end-start)));
        if(imprimir.isSelected()){
           
            saida.append(g.imprimeMatriz(bb));
            saida.append("----------------------------------------------\n");
            saida.append(g.imprimeMatriz(dd));
        }

        } catch(NumberFormatException ex){
            JOptionPane.showMessageDialog(null,ex.getMessage()+" Matriz Invalida", "Error", JOptionPane.ERROR_MESSAGE);
        }
      }
        
    }//GEN-LAST:event_matSeqActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FMatriz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FMatriz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FMatriz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FMatriz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FMatriz().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox imprimir;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField mTam;
    private javax.swing.JButton matSeq;
    private javax.swing.JComboBox numeroThread;
    private javax.swing.JTextArea saida;
    private javax.swing.JLabel tempo;
    // End of variables declaration//GEN-END:variables
}
